
const Timer = {
  /**
   * 获取系统当前时间(当天日期)
   * 格式 -> 'YY-MM-DD' 
   */
  getToday() {
    var nowdate = new Date();
    var y = nowdate.getFullYear();
    var m = nowdate.getMonth() + 1;
    var d = nowdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatToday = y + "-" + m + "-" + d;
    return formatToday;
  },

  // 获取每个月一号
  getMonthOne() {
    var myDate = new Date();
    var tYear = myDate.getFullYear()
    var tMonth = myDate.getMonth()
    tMonth = doHandleZero(tMonth + 1)

    return tYear + "-" + tMonth + "-01"
  },

  //获取系统前一周的时间
  getWeek() {
    var nowdate = new Date();
    var oneweekdate = new Date(nowdate - 7 * 24 * 3600 * 1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth() + 1;
    var d = oneweekdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatWeek = y + "-" + m + "-" + d;
    return formatWeek;
  },

  //获取系统前一个月的时间
  getMonth() {
    var nowdate = new Date();
    var oneweekdate = new Date(nowdate - 30 * 24 * 3600 * 1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth() + 1;
    var d = oneweekdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatMonth = y + "-" + m + "-" + d;
    return formatMonth;
  },

  /**
   * 获取系统前 'agoDay' 天的时间
   * @param 'agoDay' -> int
   */
  getDynamicDay(agoDay) {
    var nowdate = new Date();
    var oneweekdate = new Date(nowdate - agoDay * 24 * 3600 * 1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth() + 1;
    var d = oneweekdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatAgo = y + "-" + m + "-" + d;
    return formatAgo;
  },

  //获取某天对应的周几
  getweekday(date) {
    var weekArray = new Array("7", "1", "2", "3", "4", "5", "6");
    var week = weekArray[new Date(date).getDay()]; //注意此处必须是先new一个Date
    return week;
  },

  //将秒数转成时分秒
  secondsFormat: function (s) {
    var day = Math.floor(s / (24 * 3600)); // Math.floor()向下取整
    var hour = Math.floor((s - day * 24 * 3600) / 3600);
    var minute = Math.floor((s - day * 24 * 3600 - hour * 3600) / 60);
    var second = s - day * 24 * 3600 - hour * 3600 - minute * 60;

    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;

    return hour + ":" + minute + ":" + second;
  },

  //将秒数转成分秒
  secondsFormat2: function (s) {
    var day = Math.floor(s / (24 * 3600)); // Math.floor()向下取整
    var hour = Math.floor((s - day * 24 * 3600) / 3600);
    var minute = Math.floor((s - day * 24 * 3600 - hour * 3600) / 60);
    var second = s - day * 24 * 3600 - hour * 3600 - minute * 60;

    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;

    return minute + ":" + second;
  },


  // -------倒计时--------
  /**
   * @params {number} endtime
   * 参数是结束时间的毫秒数
   * eg: endtime = (new Date()).getTime() + 1 * 60 * 60 * 1000  当前时间的1个小时后
   * 
   * @returns {string} '00:59:59'
   */
  showtime(endtime) {
    var nowtime = new Date()  //获取当前时间

    var lefttime = endtime - nowtime.getTime()  //距离结束时间的毫秒数

    // var leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24))  //计算天数

    var lefth = Math.floor(lefttime / (1000 * 60 * 60) % 24)  //计算小时数
    lefth = lefth < 10 ? '0' + lefth : lefth

    var leftm = Math.floor(lefttime / (1000 * 60) % 60)  //计算分钟数
    leftm = leftm < 10 ? '0' + leftm : leftm

    var lefts = Math.floor(lefttime / 1000 % 60);  //计算秒数
    lefts = lefts < 10 ? '0' + lefts : lefts

    // return leftd + "天" + lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
    return lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
  }
}

export default Timer  // 所有时间类的处理方法

/**
 * @export function
 *
 * @param {Date} date 可以 new Date 出时间的任何格式  1574225337307 | 2019/08/20 | 2019/08 | new Date() | Date.now() 等
 * @param {String} formatType
 *      YYYY-MM-DD / YYYY年MM月DD日HH点NN分SS秒 WW|WWW(转化为周几， 或本周几) / {}空对象 / null
 *      YYYY 年，  MM 月， DD 日 ， HH 小时， NN 分钟 SS 秒
 *      WW 周几（汉字, 使用：'星期WW' ）  WWW 周几（汉字， 周一， 周日, 使用: DD日WWW -> 21日周三）   WWWW 本?周几（汉字， 与前者区别是如果在当周会显示本周几）
 *
 * @return {string | Object} 格式化后的日期对象 可能是字符串或者对象，受传入格式约束
 *
 */
 export function formatDate(date, formatType) {
  const _date = new Date(date);
  const current = Date.now();
  const yyyy = _date.getFullYear();
  const mm = _date.getMonth();
  const dd = _date.getDate();
  const hh = _date.getHours();
  const nn = _date.getMinutes();
  const ss = _date.getSeconds();
  const ww = _date.getDay();
  const wwMap = ['日', '一', '二', '三', '四', '五', '六'];
  const isThisWeek = isSameWeek(new Date(_date > current ? current : _date), new Date(_date <= current ? current : _date));
  if (typeof formatType === 'string') {
      return formatType.replace(/YYYY/gi, yyyy)
          .replace(/MM/gi, mm >= 9 ? mm + 1 : '0' + (mm + 1))
          .replace(/DD/gi, dd >= 10 ? dd : '0' + dd)
          .replace(/HH/gi, hh >= 10 ? hh : '0' + hh)
          .replace(/NN/gi, nn >= 10 ? nn : '0' + nn)
          .replace(/SS/gi, ss >= 10 ? ss : '0' + ss)
          .replace(/W{4}/gi, `${isThisWeek ? '本' : ''}周${wwMap[ww]}`)
          .replace(/W{3}/gi, `周${wwMap[ww]}`)
          .replace(/W{2}/gi, wwMap[ww]);
  }
  return { yyyy, mm, dd, hh, nn, ss };
}
